iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 13
1
自我挑戰組

今年我想陪著 30 天系列 第 13

今年我想陪著 30 天之 13

  • 分享至 

  • xImage
  •  

1389. Create Target Array in the Given Order

Given two arrays of integers nums and index. Your task is to create target array under the following rules:
Initially target array is empty.
From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array.
Repeat the previous step until there are no elements to read in nums and index.
Return the target array.
It is guaranteed that the insertion operations will be valid.

  • Example 1:
    Input: nums = [0,1,2,3,4], index = [0,1,2,2,1]
    Output: [0,4,1,3,2]
    Explanation:
    nums index target
    0 0 [0]
    1 1 [0,1]
    2 2 [0,1,2]
    3 2 [0,1,3,2]
    4 1 [0,4,1,3,2]

  • Example 2:
    Input: nums = [1,2,3,4,0], index = [0,1,2,3,0]
    Output: [0,1,2,3,4]
    Explanation:
    nums index target
    1 0 [1]
    2 1 [1,2]
    3 2 [1,2,3]
    4 3 [1,2,3,4]
    0 0 [0,1,2,3,4]

  • Example 3:
    Input: nums = [1], index = [0]
    Output: [1]

var createTargetArray = function(nums, index) {
    var arr = [];
    for(var i in nums) {
        arr.splice(index[i], 0, nums[i])
    }
    return arr
};

上一篇
今年我想陪著 30 天之 12
下一篇
今年我想陪著 30 天之 14
系列文
今年我想陪著 30 天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言